package com.bz.cart.controller;

import com.bz.cart.service.CookieCartService;
import com.bz.cart.service.RedisCartService;
import com.bz.cart.service.UserCheckService;
import com.bz.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CookieCartService cookieCartService;
    @Autowired
    private RedisCartService redisCartService;
    @Autowired
    private UserCheckService userCheckService;

    @PostMapping("/addItem")
    public Result addItem(Long itemId, String userId, @RequestParam(defaultValue = "1") Integer num, HttpServletRequest request, HttpServletResponse response){
        if (userId == null){
            //在用户未登录的状态下
            return cookieCartService.addItem(itemId,num,request,response);
        }else {
            // 在用户已登录的状态
            return redisCartService.addItem(itemId,num,userId);
        }
    }
}
